package com.atguigu.tingshu.album.mapper;

import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface TrackInfoMapper extends BaseMapper<TrackInfo> {


    IPage<TrackListVo> selectUserTrackPage(Page<TrackListVo> infoPage, @Param("vo") TrackInfoQuery trackInfoQuery);

    @Update("update track_info set order_num=order_num-1 where album_id=#{albumId} and order_num>#{orderNum} and is_deleted=0")
    int updateOrderNum(@Param("albumId") Long albumId, @Param("orderNum") Integer orderNum);


    @Select("select ti.id as track_id,\n" +
            "       ti.track_title,\n" +
            "       ti.order_num,\n" +
            "       ti.media_duration,\n" +
            "       max(if(stat_type='0701',stat_num,0)) playStatNum,\n" +
            "       max(if(stat_type='0704',stat_num,0)) commentStatNum,\n" +
            "       ti.create_time\n" +
            "from track_info ti inner join track_stat ts on ti.id=ts.track_id\n" +
            "where ti.album_id=#{albumId} and ti.status='0501' and ti.is_deleted=0 and ts.is_deleted=0\n" +
            "group by ti.id")
    IPage<AlbumTrackListVo> selectAlbumTrackPage(Page<AlbumTrackListVo> infoPage,@Param("albumId") Long albumId);

    @Update("update track_stat set stat_num=stat_num+#{count}, update_time = now() where track_id=#{trackId} and stat_type=#{trackStatPlay} and is_deleted=0;")
    Integer updateStat(@Param("trackId") Long trackId,@Param("count") Integer count,@Param("trackStatPlay") String trackStatPlay);
}
